package com.tao.work;

import com.rabbitmq.client.*;
import com.utils.RabbitMQUtils;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName ConsumerTwo
 * @Author HETAO
 * @Date 2020/5/4 20:07
 */
public class ConsumerTwo {
    public static void main(String[] args) throws IOException {
        Connection connection = RabbitMQUtils.getConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare("work", true, false, false, null);
        //每次消费一个
        channel.basicQos(1);
        //不自动确认
        channel.basicConsume("work", false, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者-2:  body = " + new String(body));
                //手动确认,参数1:手动确认消息标识 参数2:每次确认一个
                channel.basicAck(envelope.getDeliveryTag(), false);
            }
        });
    }
}
